//
//  BGMPushView.m
//  BGM
//
//  Created by 我要变好看 on 2018/12/29.
//  Copyright © 2018 com.alirtc.demo. All rights reserved.
//

#import "BGMPushView.h"
#import "Common.h"
#import "PushCollectionViewCell.h"

@interface BGMPushView ()<UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,PushCollectionViewCellDelegate>
@property (nonatomic, strong)NSArray *dataArray;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)UIView *bgView1;
@property (nonatomic, strong)UIButton *pushBtn;


@end


@implementation BGMPushView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self != nil) {
        self.backgroundColor = [UIColor clearColor];
        
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH)];
        bgView.backgroundColor = [UIColor blackColor];
        bgView.alpha = 0.5;
        [self addSubview:bgView];
        
       
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickBgView)];
        [bgView addGestureRecognizer:tap];
        
        
        
        
        self.bgView1 = [[UIView alloc]initWithFrame:CGRectZero];
        self.bgView1.backgroundColor = [UIColor whiteColor];
        self.bgView1.layer.cornerRadius = 30;
        self.bgView1.layer.masksToBounds = YES;
        [self addSubview:self.bgView1];
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.bgView1 addSubview:self.collectionView];
        [self.collectionView registerClass:[PushCollectionViewCell class] forCellWithReuseIdentifier:@"PushCollectionViewCell"];
        
        
        self.pushBtn =[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
        [self.pushBtn setImage:[UIImage imageNamed:@"tabbar_2"] forState:UIControlStateNormal];
        [self.pushBtn addTarget:self action:@selector(clickPushBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView1 addSubview:self.pushBtn];
    }
    return self;
}

- (void)clickPushBtn{

    [UIView animateWithDuration:0.1 animations:^{
          self.bgView1.frame = CGRectMake(0, SCREENH, SCREENW, self.bgView1.frame.size.height);
    } completion:^(BOOL finished) {
         [self.pushViewDelegate clickPushBgView];
    }];
}
- (void)showPushView{
    [UIView animateWithDuration:0.1 animations:^{
        self.bgView1.frame = CGRectMake(0, SCREENH-self.bgView1.frame.size.height+50, SCREENW, self.bgView1.frame.size.height);
        
    } completion:^(BOOL finished) {
       
    }];
}
- (void)clickBgView{
    [self clickPushBtn];
}

- (void)getTypeArray:(NSArray *)array{
    self.dataArray = array;
    int section =  (int) ceil(self.dataArray.count / 4.0);//向上取
    float H = section*(SCREENW/4+20);
    self.bgView1.frame = CGRectMake(0, SCREENH, SCREENW,H+150);
    self.pushBtn.center = CGPointMake(SCREENW/2, self.bgView1.frame.size.height-100);
    self.collectionView.frame = CGRectMake(0, 0, SCREENW, H);
    [self.collectionView reloadData];
    
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifer = @"PushCollectionViewCell" ;
    PushCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifer forIndexPath:indexPath];
    if(cell == nil){
        cell = [[PushCollectionViewCell alloc]initWithFrame:CGRectMake(0, 0, SCREENW/4, SCREENW/4+20)];
    }
    cell.pushCellDelegate = self;
    NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.row];
    cell.dic = dic;
    [cell.iconBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"push_%d",(int)indexPath.row+1]] forState:UIControlStateNormal];
    cell.iconTitle.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"name"]];
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(SCREENW/4, SCREENW/4+20);
}
//
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0,0,0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
//-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    return  YES;
//}
- (void)clickCell:(NSDictionary *)dic{
    NSDictionary *postDic = @{@"dic":dic,@"array":self.dataArray};
    [[NSNotificationCenter defaultCenter]postNotificationName:@"APP_PUSHMESSAGE" object:postDic];
    [self clickPushBtn];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
